<?php


namespace App\library\services;


use App\Models\FangAttrs;

class FangAttrsService extends BaseServices
{
    /**
     * Author:YangXinPeng
     * 获取房源属性 朝向
     * @return mixed
     */
    public static function getFangDirection()
    {
        $fangAttr = FangAttrs::where('field_name', 'fang_direction')->first()->toArray();
        $pid = $fangAttr['id'];
        $directions = FangAttrs::where('pid', $pid)->get()->toArray();
        return $directions;
    }

    /**
     * Author:YangXinPeng
     * 获取户型数据
     * @return mixed
     */
    public static function getHouseType()
    {
        $type = FangAttrs::where('field_name', 'house_type')->first()->toArray();
        $pid = $type['id'];
        $types = FangAttrs::where('pid', $pid)->get()->toArray();
        return $types;
    }

    /**
     * Author:YangXinPeng
     * 获取装修程度数据
     * @return mixed
     */
    public static function getFangFit()
    {
        $type = FangAttrs::where('field_name', 'fang_fit')->first()->toArray();
        $pid = $type['id'];
        $types = FangAttrs::where('pid', $pid)->get()->toArray();
        return $types;
    }

    /**
     * Author:YangXinPeng
     * 获取租赁方式数据
     * @return mixed
     */
    public static function getFangRent()
    {
        $type = FangAttrs::where('field_name', 'fang_rent_class')->first()->toArray();
        $pid = $type['id'];
        $types = FangAttrs::where('pid', $pid)->get()->toArray();
        return $types;
    }

    /**
     * Author:YangXinPeng
     * 获取配置设施数据
     * @return mixed
     */
    public static function getFangConfig()
    {
        $type = FangAttrs::where('field_name', 'fang_config')->first()->toArray();
        $pid = $type['id'];
        $types = FangAttrs::where('pid', $pid)->get()->toArray();
        return $types;
    }

    /**
     * Author:YangXinPeng
     * 根据ID获取数据信息
     * @param $id
     * @return array
     */
    public static function getAttrName($arr)
    {
        if (empty($arr)) {
            return self::fail('参数错误');
        }
        $result = [];
        foreach ($arr as $value) {
            $data = FangAttrs::find($value);
            $data && $data = $data->toArray();
            $result[] = $data;
        }
        return $result;
    }

    /**
     * Author:william
     * 获取房源属性 朝向
     * @return mixed
     */
    public static function direction($where = [])
    {
        $fangAttr = FangAttrs::where('field_name', 'fang_direction')->first();
        $fangAttr && $fangAttr = $fangAttr->toArray();
        $data = self::getFangAttr($fangAttr, $where);
        return $data;
    }

    /**
     * Author:william
     * 获取户型数据
     * @return mixed
     */
    public static function houseType($where = [])
    {
        $type = FangAttrs::where('field_name', 'house_type')->first();
        $type && $type = $type->toArray();
        $data = self::getFangAttr($type, $where);
        return $data;
    }

    /**
     * Author:william
     * 获取装修程度数据
     * @return mixed
     */
    public static function fangFit($where = [])
    {
        $type = FangAttrs::where('field_name', 'fang_fit')->first();
        $type && $type = $type->toArray();
        $data = self::getFangAttr($type, $where);
        return $data;
    }

    /**
     * Author:william
     * 获取租赁方式数据
     * @return mixed
     */
    public static function fangRent($where = [])
    {
        $type = FangAttrs::where('field_name', 'fang_rent_class')->first();
        $type && $type = $type->toArray();
        $data = self::getFangAttr($type, $where);
        return $data;
    }

    /**
     * Author:william
     * 获取配置设施数据
     * @return mixed
     */
    public static function fangConfig($where = [])
    {
        $type = FangAttrs::where('field_name', 'fang_config')->first();
        $type && $type = $type->toArray();
        $data = self::getFangAttr($type, $where);
        return $data;
    }

    /**
     * Author:YangXinPeng
     * 售价条件
     * @return array
     */
    public static function fangPrice()
    {
        $type = FangAttrs::where('field_name','fang_price')->first();
        $type && $type = $type->toArray();
        $pid = $type['id'];
        $types = FangAttrs::where('pid', $pid)->get()->toArray();
        return self::success('成功',$types);
    }

    /**
     * Author:YangXinPeng
     * 获取房源属性
     * @param array $fangAttr
     * @param array $where
     * @return mixed
     */
    public static function getFangAttr($fangAttr = [], $where = [])
    {
        $pid = $fangAttr['id'];
        if (!empty($where)) {
            $data = FangAttrs::where('pid', $pid)->get($where);
        } else {
            $data = FangAttrs::where('pid', $pid)->get();
        }
        $data && $data = $data->toArray();
//        foreach ($data as $key => $val) {
//            $index[] = $val['id'];
//            $value[] = $val['name'];
//        }
//        $arr = array_combine($index, $value);
        return $data;
    }
}
